package xcmg.device.service.borrowMaterial;

import com.github.pagehelper.PageInfo;
import yb.ecp.fast.infra.util.PageHelperPlus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xcmg.device.dao.entity.borrowMaterial.BorrowMaterialDetailDO;
import xcmg.device.dao.mapper.borrowMaterial.BorrowMaterialDetailMapper;
import xcmg.device.infra.ErrorCode;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.List;

/**
 * @author ZHANGK
 * @since  2020/6/11
 **/
@Slf4j
@Service
public class BorrowMaterialDetailService {

    @Autowired
    BorrowMaterialDetailMapper borrowMaterialDetailMapper;

    @Autowired
    FastGenClient fastGenClient;

    @Transactional(rollbackFor = Exception.class)
    public ErrorCode insert(BorrowMaterialDetailDO borrowMaterialDetailDO){
        ActionResult<String> guidResult = fastGenClient.textGuid();
        if (guidResult == null || guidResult.getCode() != ErrorCode.Success.getCode()) {
            log.error("主键生成失败！");
            return ErrorCode.Failure;
        }
        borrowMaterialDetailDO.setId(guidResult.getValue());
        boolean result = borrowMaterialDetailMapper.insert(borrowMaterialDetailDO);
        if (result) {
            return ErrorCode.Success;
        }
        return ErrorCode.Failure;
    }

    @Transactional(rollbackFor = Exception.class)
    public ErrorCode deleteById(BorrowMaterialDetailDO borrowMaterialDetailDO){
        boolean result = borrowMaterialDetailMapper.deleteById(borrowMaterialDetailDO);
        if (result) {
            return ErrorCode.Success;
        }
        return ErrorCode.Failure;
    }

    @Transactional(rollbackFor = Exception.class)
    public ErrorCode updateById(BorrowMaterialDetailDO borrowMaterialDetailDO){
        boolean result = borrowMaterialDetailMapper.updateById(borrowMaterialDetailDO);
        if (result) {
            return ErrorCode.Success;
        }
        return ErrorCode.Failure;
    }

    public PageCommonVO<BorrowMaterialDetailDO> selectList(SearchCommonVO<BorrowMaterialDetailDO> condition){
        PageCommonVO<BorrowMaterialDetailDO> pageCommonVO = new PageCommonVO<>();
        List<BorrowMaterialDetailDO> list = borrowMaterialDetailMapper.selectList(condition.getFilters());
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());
        pageCommonVO.setPageInfo(new PageInfo<>(list));
        pageCommonVO.setPageInfoList(list);
        return pageCommonVO;
    }

    public BorrowMaterialDetailDO selectById(String id) {
        return borrowMaterialDetailMapper.selectById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public ErrorCode batchInsert(List<BorrowMaterialDetailDO> borrowMaterialDetailDOList){
        boolean result = borrowMaterialDetailMapper.batchInsert(borrowMaterialDetailDOList);
        if (result) {
            return ErrorCode.Success;
        }
        return ErrorCode.Failure;
    }

    public List<BorrowMaterialDetailDO> selectList(BorrowMaterialDetailDO borrowMaterialDetailDO) {
        return borrowMaterialDetailMapper.selectList(borrowMaterialDetailDO);
    }

    @Transactional(rollbackFor = Exception.class)
    public ErrorCode batchUpdate(List<BorrowMaterialDetailDO> borrowMaterialDetailDOList) {
        boolean result = borrowMaterialDetailMapper.batchUpdate(borrowMaterialDetailDOList);
        if (result) {
            return ErrorCode.Success;
        }
        return ErrorCode.Failure;
    }

    @Transactional(rollbackFor = Exception.class)
    public ErrorCode delete(BorrowMaterialDetailDO borrowMaterialDetailDO){
        boolean result = borrowMaterialDetailMapper.delete(borrowMaterialDetailDO);
        if (result) {
            return ErrorCode.Success;
        }
        return ErrorCode.Failure;
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteByBorrowId(List<String> ids) {
        return borrowMaterialDetailMapper.batchDeleteByBorrowId(ids);
    }
}
